package com.example.home.rubbish.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.rubbish.adapter.ClassifyDetailAdapter;
import com.example.home.rubbish.adapter.NewsClassifyAdapter;
import com.example.home.rubbish.bean.NewsClassifyDetail;
import com.example.home.rubbish.bean.NewsImgUrlBean;
import com.example.home.rubbish.bean.RcvListBean;
import com.example.home.tool.OKHttpTool;

import static com.example.home.tool.StaticActi.ip;

public class NewsClassifyActivity extends AppCompatActivity {
    private ImageView back;
    private ImageView imgUrl;
    private RecyclerView rcvList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_classify);
        initView();
        getImgUrl();
        getRcvList();
    }

    private void getImgUrl() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/poster/list",
                OKHttpTool.GET,
                NewsImgUrlBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        NewsImgUrlBean newsImgUrlBean = (NewsImgUrlBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Glide.with(NewsClassifyActivity.this).load(ip+newsImgUrlBean.getData().get(0).getImgUrl()).into(imgUrl);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void getRcvList() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/garbage-example/list",
                OKHttpTool.GET,
                RcvListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RcvListBean rcvListBean = (RcvListBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(rcvListBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindList(RcvListBean rcvListBean) {
        NewsClassifyAdapter newsRcvListAdapter = new NewsClassifyAdapter(NewsClassifyActivity.this, rcvListBean.getRows());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(NewsClassifyActivity.this, 4);
        rcvList.setLayoutManager(gridLayoutManager);
        rcvList.setAdapter(newsRcvListAdapter);
    }

    public void onRecyclableClick(View view) {
           String name = "可回收物";
            showToast(name);
        }

        public void onHazardousClick(View view) {
            String name = "有害垃圾";
            showToast(name);
        }

        public void onOrganicClick(View view) {
            String name = "湿垃圾";
            showToast(name);

        }

        public void onDryClick(View view) {
            String name = "干垃圾";
            showToast(name);
        }

        private void showToast(String message) {
            OKHttpTool.doNetRequest("prod-api/api/garbage-classification/garbage-classification/list?pageNum=1&pageSize=10&name="+message,
                    OKHttpTool.GET,
                    NewsClassifyDetail.class,
                    new OKHttpTool.NetCallback() {
                        @Override
                        public void onSuccess(Object obj) {
                            NewsClassifyDetail newsClassifyDetail = (NewsClassifyDetail) obj;
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (newsClassifyDetail.getTotal() != 0){
                                        Intent intent = new Intent(NewsClassifyActivity.this,NewsClassifyDetailActivity.class);
                                        intent.putExtra("classify",String.valueOf(newsClassifyDetail.getRows().get(0).getName()));
                                        startActivity(intent);
                                    }else {
                                        Toast.makeText(NewsClassifyActivity.this,"暂无该分类详细",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }

                        @Override
                        public void onFailure(String msg) {

                        }
                    });
        }

    private void bindShow(NewsClassifyDetail newsClassifyDetail) {
        ClassifyDetailAdapter newsRcvListAdapter = new ClassifyDetailAdapter(NewsClassifyActivity.this, newsClassifyDetail.getRows());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(NewsClassifyActivity.this, 4);
        rcvList.setLayoutManager(gridLayoutManager);
        rcvList.setAdapter(newsRcvListAdapter);
    }

    private void initView() {

        back = findViewById(R.id.back);
        imgUrl = findViewById(R.id.imgUrl);
        rcvList = findViewById(R.id.rcv_list);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewsClassifyActivity.this.finish();
            }
        });
    }
}